/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Ventanas.Cliente;

import com.asistencia.DAO.factory.FactoryDAO;
import com.asistencia.DAO.utils.Reloj;
import com.asistencia.model.Funcionario;
import com.asistencia.model.Modulo;
import com.asistencia.model.Turno;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author rfarias
 */
public class ConsultaTUR extends javax.swing.JFrame {

    static Funcionario funtemp;
    /**
     * Creates new form ConsultaTUR
     */
    public ConsultaTUR(Funcionario fun) {
        
            initComponents();
            funtemp= fun;
            String str = this.jLabel1.getText();
            str = str.replace("#", funtemp.getNombres());
            this.jLabel1.setText(str);
            cargaTabla();
    }

    /**
     * Carga los datos de turnos en la tabla tblTurno
     * para el usuario actual.
     * 
     */
    private void cargaTabla()
    {
            DefaultTableModel modelot = (DefaultTableModel) tblTurno.getModel();
            List<Modulo> modul = FactoryDAO.getInstance().getDataMod().findAll();
            Reloj rel  =new Reloj();
            String fdb=rel.fechaDisp();
            String fechaini = rel.fechaDB();
            Date fini;
            String fechafin;
            SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
            java.util.Date utilDate = new java.util.Date();
            java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
            fini= sqlDate;
            Calendar cal = Calendar.getInstance(); 
            cal.setTime(fini); 
            cal.add(Calendar.MONTH, 1);
            cal.add(Calendar.DATE, 5);
            fechafin = cal.get(Calendar.YEAR)+"-"+(cal.get(Calendar.MONTH)>9?""+cal.get(Calendar.MONTH):"0"+cal.get(Calendar.MONTH))+"-"+(cal.get(Calendar.DAY_OF_MONTH)>9?""+cal.get(Calendar.DAY_OF_MONTH):"0"+cal.get(Calendar.DAY_OF_MONTH));
            Turno t;
            Modulo m;
            
        for (Iterator<Modulo> im = modul.iterator(); im.hasNext();) {
            m  = im.next();
            List<Turno> tur = FactoryDAO.getInstance().getDataTur().findAll(fechaini, fechafin, m, funtemp);
            for (Iterator<Turno> it = tur.iterator(); it.hasNext();) 
            {
                String funcio=null;
                t  = it.next();
                if (t!=null)
                {
                    if (t.getFunc()!=null)
                    {
                      funcio = t.getFunc().getNombres();
                    }
                    Object[] linea = new Object[5];
                    linea[0] = m.getDescripcion() ;
                    linea[1] = t.getHora_ini().toString();
                    linea[2] = t.getHora_fin().toString();
                    linea[3] = t.getFecha_desde().toString();
                    linea[4] = t.getFecha_hasta().toString();
                    modelot.addRow(linea);
                            
                }
            }           
        }
    
    }
    
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnSalir = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblTurno = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("SITA");
        setBounds(new java.awt.Rectangle(200, 200, 0, 0));
        setIconImage(new ImageIcon(getClass().getResource("/Ventanas/icono.png")).getImage());
        setIconImages(null);

        btnSalir.setFont(new java.awt.Font("sansserif", 0, 10)); // NOI18N
        btnSalir.setText("Salir");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Detalle de turnos para el funcionario #");

        tblTurno.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Modulo", "Entrada", "Salida", "Desde", "Hasta"
            }
        ));
        jScrollPane1.setViewportView(tblTurno);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 880, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnSalir)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSalir))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnSalirActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ConsultaTUR.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ConsultaTUR.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ConsultaTUR.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ConsultaTUR.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ConsultaTUR(funtemp).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSalir;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblTurno;
    // End of variables declaration//GEN-END:variables
}
